﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HtmlControlsUnitTest.cs" company="">
// </copyright>
// <summary>
//   Summary description for HtmlControlsUnitTest
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.CuitTestProject
{
    using System;

    using Microsoft.VisualStudio.TestTools.UITest.Extension;
    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using UITest.CUIT.CuitControls.HtmlControls;

    /// <summary>
    ///     Summary description for HtmlControlsUnitTest
    /// </summary>
    [CodedUITest]
    
    public class HtmlControlsUnitTest : UITestingBaseClass
    {
        #region Fields

        //private readonly string CurrentDirectory = Directory.GetCurrentDirectory();

       
        #endregion

        // You can use the following additional attributes as you write your tests:

        ////Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize()
        // {        
        // // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        // }

        ////Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup()
        // {        
        // // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        // }
        #region Public Properties

        /// <summary>
        ///     Gets or sets the test context which provides
        ///     information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     The base class get parent microsoft control.
        /// </summary>
        [TestCategory("BaseClass")]
        [TestMethod]
        public void BaseClassGetParentMicrosoftControl()
        {
            this.LaunchTestingPage();
            CuitHtmlEdit control = this.UITestHtmlPage.html5ComboBoxEdit;
            Assert.AreEqual("comboboxDivhtml5", control.GetParentMicrosoftControl().GetProperty("Id"));
        }

        /// <summary>
        ///     The base class parent.
        /// </summary>
        [TestCategory("BaseClass")]
        [TestMethod]
        public void BaseClassParent()
        {
            this.LaunchTestingPage();
            CuitHtmlEdit control = this.UITestHtmlPage.html5ComboBoxEdit;
            Assert.AreEqual("comboboxDivhtml5", control.Parent.GetProperty("Id"));

            Console.WriteLine(control.Parent.GetBaseType());
        }

        /// <summary>
        ///     The html button click succeeds.
        /// </summary>
        [TestCategory("HtmlButton")]
        [TestMethod]
        public void HtmlButtonClickSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlButton control = this.UITestHtmlPage.htmlButton;
            control.Click();
        }

        /// <summary>
        ///     The html checkbox check succeeds.
        /// </summary>
        [TestCategory("HtmlCheckbox")]
        [TestMethod]
        public void HtmlCheckboxCheckSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlCheckBox controlA = this.UITestHtmlPage.htmlCheckboxA;
            CuitHtmlCheckBox controlB = this.UITestHtmlPage.htmlCheckboxB;

            // Set Default Value
            Assert.IsFalse(controlA.Checked);
            Assert.IsTrue(controlB.Checked);

            // Checked the checkbox
            controlA.Set();
            controlB.Set();

            Assert.IsTrue(controlA.Checked);
            Assert.IsTrue(controlB.Checked);
        }

        /// <summary>
        ///     The html checkbox uncheck succeeds.
        /// </summary>
        [TestCategory("HtmlCheckbox")]
        [TestMethod]
        public void HtmlCheckboxUnCheckSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlCheckBox controlA = this.UITestHtmlPage.htmlCheckboxA;
            CuitHtmlCheckBox controlB = this.UITestHtmlPage.htmlCheckboxB;

            // Set Default Value
            Assert.IsFalse(controlA.Checked);
            Assert.IsTrue(controlB.Checked);

            // Checked the checkbox
            controlA.Clear();
            controlB.Clear();

            Assert.IsFalse(controlA.Checked);
            Assert.IsFalse(controlB.Checked);
        }

        /// <summary>
        ///     The html combo box html 4 select succeeds.
        /// </summary>
        [TestCategory("HtmlComboBox")]
        [TestMethod]
        public void HtmlComboBoxHtml4SelectSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlComboBox control = this.UITestHtmlPage.html4Combobox;

            Assert.IsTrue(control.SelectedItem == "one");
            Assert.AreEqual(0, control.SelectedIndex);
            Assert.AreEqual(3, control.ItemCount);
            control.SelectItem(1);
            Assert.IsTrue(control.SelectedItem == "two");
            control.SelectItem("three");
            Assert.IsTrue(control.SelectedItem == "three");
        }

        /// <summary>
        ///     Html 5 code is not support
        /// </summary>
        [TestCategory("HtmlComboBox")]
        [TestMethod]
        public void HtmlComboBoxHtml5SelectSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlEdit controlA = this.UITestHtmlPage.html5ComboBoxEdit;

            // var controlB = this.UITestHtmlPage.html5ComboBoxList;
            controlA.Typing("two");

            // controlB.DrawHighlight();
            Keyboard.SendKeys("{ENTER}");
        }

        /// <summary>
        ///     The html edit set text succeeds.
        /// </summary>
        [TestCategory("HtmlEdit")]
        [TestMethod]
        public void HtmlEditSetTextSucceeds()
        {
            string inputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlEdit control = this.UITestHtmlPage.htmlEdit;

            Assert.IsFalse(control.ReadOnly);
            control.SetText(inputText);
            Assert.AreEqual(inputText, control.GetText());
        }

        /// <summary>
        ///     The html edit typing succeeds.
        /// </summary>
        [TestCategory("HtmlEdit")]
        [TestMethod]
        public void HtmlEditTypingSucceeds()
        {
            string inputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlEdit control = this.UITestHtmlPage.htmlEdit;

            Assert.IsFalse(control.ReadOnly);
            control.Typing(inputText);
            Assert.AreEqual(inputText, control.GetText());
        }

        /// <summary>
        ///     The html edit under div set text succeeds.
        /// </summary>
        [TestCategory("HtmlEditUnderDiv")]
        [TestMethod]
        public void HtmlEditUnderDivSetTextSucceeds()
        {
            string inputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlEditUnderDiv controlA = this.UITestHtmlPage.htmlEditUnderDiv;
            CuitHtmlEdit controlB = this.UITestHtmlPage.htmlEdit;

            controlA.SetText(inputText);
            Assert.AreEqual(inputText, controlA.GetText());

            Assert.AreEqual(controlA.GetText(), controlB.GetText());
        }

        /// <summary>
        ///     The html edit under div typing succeeds.
        /// </summary>
        [TestCategory("HtmlEditUnderDiv")]
        [TestMethod]
        public void HtmlEditUnderDivTypingSucceeds()
        {
            string inputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlEditUnderDiv controlA = this.UITestHtmlPage.htmlEditUnderDiv;

            controlA.Typing(inputText);
            Assert.AreEqual(inputText, controlA.GetText());
        }

        /// <summary>
        ///     The html file input set file succeeds.
        /// </summary>
        [TestCategory("HtmlFileInput")]
        [TestMethod]
        public void HtmlFileInputSetFileSucceeds()
        {
            string filepath = this.CurrentDirectory + "\\TestHtmlPage.html";
            this.LaunchTestingPage();
            CuitHtmlFileInput control = this.UITestHtmlPage.htmlfileInput;

            control.SetFile(filepath);
            Assert.AreEqual(filepath, control.UnWrap().ValueAttribute);
        }

        /*
        /// EditableDiv is not working even on microsoft control
        /// <summary>
        /// The html editable div_ set text_ succeeds.
        ///
        /// </summary>
        [TestCategory("HtmlEditableDiv")]
        [TestMethod]
        public void HtmlEditableDiv_SetText_Succeeds()
        {
            string inputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlEditableDiv control = this.UITestHtmlPage.htmlEditableDiv;

            //Assert.IsFalse(control.UnWrap().ReadOnly);
            //control.SetText(inputText);
            //Assert.AreEqual(inputText, control.GetText());
            

        }
         */

        /// <summary>
        ///     The html input button click succeeds.
        /// </summary>
        [TestCategory("HtmlInputButton")]
        [TestMethod]
        public void HtmlInputButtonClickSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlInputButton control = this.UITestHtmlPage.htmlInputButton;
            control.Click();
            this.UITestHtmlPage.browserWindow.PerformDialogAction(BrowserDialogAction.Ok);
        }

        /// <summary>
        ///     The html label label for succeeds.
        /// </summary>
        [TestCategory("HtmlLabel")]
        [TestMethod]
        public void HtmlLabelLabelForSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlLabel control = this.UITestHtmlPage.htmlLabel;

            Assert.AreEqual("male", control.LabelFor);
        }

        /// <summary>
        ///     The html list item exists.
        /// </summary>
        [TestCategory("HtmlList")]
        [TestMethod]
        public void HtmlListItemExists()
        {
            const string Item = "three";
            this.LaunchTestingPage();
            CuitHtmlList control = this.UITestHtmlPage.htmlListbox;

            Assert.IsTrue(control.ItemExists(Item));
        }

        /// <summary>
        ///     The html list selected item as string succeeds.
        /// </summary>
        [TestCategory("HtmlList")]
        [TestMethod]
        public void HtmlListSelectedItemAsStringSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlList control = this.UITestHtmlPage.htmlListbox;

            Assert.AreEqual("two", control.SelectedItemAsString);
            control.SelectedItemAsString = "three";
            Assert.AreEqual("three", control.SelectedItemAsString);
        }

        /// <summary>
        ///     The html list selected item succeeds.
        /// </summary>
        [TestCategory("HtmlList")]
        [TestMethod]
        public void HtmlListSelectedItemSucceeds()
        {
            string[] listItem = { "one", "four" };
            this.LaunchTestingPage();
            CuitHtmlList control = this.UITestHtmlPage.htmlListbox;

            CollectionAssert.AreEqual(new[] { "two" }, control.SelectedItems);
            control.SelectedItems = listItem;
            CollectionAssert.AreEqual(listItem, control.SelectedItems);
        }

        /// <summary>
        ///     The html radio button select succeeds.
        /// </summary>
        [TestCategory("HtmlRadioButton")]
        [TestMethod]
        public void HtmlRadioButtonSelectSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlRadioButton controlA = this.UITestHtmlPage.htmlRadioButtonMale;
            CuitHtmlRadioButton controlB = this.UITestHtmlPage.htmlRadioButtonFemale;

            Assert.IsTrue(controlB.IsSelected);
            controlA.Select();
            Assert.IsTrue(controlA.IsSelected);
        }

    

     

        /// <summary>
        ///     The html table click on cell succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableClickOnCellSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithAlertOnHeadTable;
            control.ClickOnCell(1, 0);

            string popupText = this.UITestHtmlPage.GetAlertPopUpWinText().DisplayText;
            Assert.AreEqual("Cell 1", popupText);
            this.UITestHtmlPage.browserWindow.PerformDialogAction(BrowserDialogAction.Ok);
        }

        /// <summary>
        ///     The html table click on header cell with header name succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableClickOnHeaderCellWithHeaderNameSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithAlertOnHeadTable;
            control.ClickOnHeaderCell("Header1");

            string popupText = this.UITestHtmlPage.GetAlertPopUpWinText().DisplayText;
            Assert.AreEqual("Header1", popupText);
            this.UITestHtmlPage.browserWindow.PerformDialogAction(BrowserDialogAction.Ok);
        }

        /// <summary>
        ///     The html table click on header cell with row and column index succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableClickOnHeaderCellWithRowAndColumnIndexSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithAlertOnHeadTable;
            control.ClickOnHeaderCell(0, 0);

            string popupText = this.UITestHtmlPage.GetAlertPopUpWinText().DisplayText;
            Assert.AreEqual("Header1", popupText);
            this.UITestHtmlPage.browserWindow.PerformDialogAction(BrowserDialogAction.Ok);
        }

        /// <summary>
        ///     The html table column count succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableColumnCountSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            Assert.AreEqual(3, control.ColumnCount);
        }

        /// <summary>
        ///     The html table double click on cell succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableDoubleClickOnCellSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithAlertOnHeadTable;
            control.DoubleClickOnCell(2, 0);

            string popupText = this.UITestHtmlPage.GetAlertPopUpWinText().DisplayText;
            Assert.AreEqual("Cell 2", popupText);
            this.UITestHtmlPage.browserWindow.PerformDialogAction(BrowserDialogAction.Ok);
        }

        /// <summary>
        ///     The html table find row with column index succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableFindRowWithColumnIndexSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            int rowIndex = control.FindRow(1, "5", CuitHtmlTableSearchOptions.StartsWith);

            Assert.AreEqual(2, rowIndex);
        }

        /// <summary>
        ///     The html table find row with header name fail.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableFindRowWithHeaderNameFail()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            int rowIndex = control.FindRow("Header3", "5", CuitHtmlTableSearchOptions.StartsWith);

            Assert.AreEqual(-1, rowIndex);
        }

        /// <summary>
        ///     The html table find row with header name succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableFindRowWithHeaderNameSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            int rowIndex = control.FindRow("Header2", "5", CuitHtmlTableSearchOptions.StartsWith);

            Assert.AreEqual(2, rowIndex);
        }

        /// <summary>
        ///     The html table get cell child html checkbox succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableGetCellChildHtmlCheckboxSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithSpecialCell;
            var controlEditA = control.GetCellChild<CuitHtmlCheckBox>(2, "Header Checkbox");
            var controlEditB = new CuitHtmlCheckBox(control.GetParentMicrosoftControl(), "Name=table_chkbox2");

            Assert.IsFalse(controlEditA.Checked);
            controlEditA.Set();

            Assert.IsTrue(controlEditA.Checked);
            Assert.IsTrue(controlEditB.Checked);
        }

        /// <summary>
        ///     The html table get cell child html edit succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableGetCellChildHtmlEditSucceeds()
        {
            const string InputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithSpecialCell;
            var controlEditA = control.GetCellChild<CuitHtmlEdit>(2, "Header Edit");

            controlEditA.SetText(InputText);
            var controlEditB = new CuitHtmlEdit(control.GetParentMicrosoftControl(), "Name=table_text2");

            Assert.AreEqual(InputText, controlEditA.GetText());
            Assert.AreEqual(InputText, controlEditB.GetText());
        }

        /// <summary>
        ///     The html table get cell child with 2 html edit succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableGetCellChildWith2HtmlEditSucceeds()
        {
            const string InputText = "ABC";
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithSpecialCell;
            var controlEditA = control.GetCellChild<CuitHtmlEdit>(2, "Header 2 Edit", "name=table_textA");
            var controlEditB = control.GetCellChild<CuitHtmlEdit>(2, "Header 2 Edit", "name=table_textB");


            controlEditA.SetText(InputText);
            controlEditB.SetText(InputText + InputText);
            Assert.AreEqual(InputText, controlEditA.GetText());
            Assert.AreEqual(InputText + InputText, controlEditB.GetText());
        }

        /// <summary>
        ///     The html table get column header succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableGetColumnHeaderSucceeds()
        {
            string[] expectedValues = { "Header1", "Header2", "Header3" };

            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            string[] headers = control.GetColumnHeaders();
            CollectionAssert.AreEqual(expectedValues, headers);
        }

        /// <summary>
        ///     The html table get column headers succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableGetColumnHeadersSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            string[] expectedValues = { "Header1", "Header2", "Header3" };
            string[] headers = control.GetColumnHeaders();
            Assert.AreEqual(expectedValues[0], headers[0]);
            Assert.AreEqual(expectedValues[1], headers[1]);
            Assert.AreEqual(expectedValues[2], headers[2]);
        }

        /// <summary>
        ///     The html table get row header succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableGetRowHeaderSucceeds()
        {
            string[] expectedValues = { "header1", "header2", "header3" };

            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaderCells;
            string[] headers = control.GetRowHeaders();
            CollectionAssert.AreEqual(expectedValues, headers);
        }

        /// <summary>
        ///     The html table row count succeeds.
        /// </summary>
        [TestCategory("HtmlTable")]
        [TestMethod]
        public void HtmlTableRowCountSucceeds()
        {
            this.LaunchTestingPage();
            CuitHtmlTable control = this.UITestHtmlPage.htmlTableWithHeaders;
            Assert.AreEqual(4, control.RowCount);
        }

        /// <summary>
        ///     The html text area set text succeeds.
        /// </summary>
        [TestCategory("HtmlTextArea")]
        [TestMethod]
        public void HtmlTextAreaSetTextSucceeds()
        {
            const string InputText = "Testing Text Area";
            this.LaunchTestingPage();
            CuitHtmlTextArea control = this.UITestHtmlPage.htmlTextArea;

            Assert.IsFalse(control.ReadOnly);
            control.SetText(InputText);
            Assert.AreEqual(InputText, control.Text);
            Assert.AreEqual(InputText, control.GetText());
        }

        #endregion

        #region Methods

       

        #endregion


    }
}